package vn.mahjonggame.zone.event;

import java.util.ArrayList;
import java.util.List;

import vn.mahjonggame.metadata.GameActionType;
import vn.mahjonggame.metadata.UserVarName;
import vn.mahjonggame.platform.GameplayConfig;
import vn.mahjonggame.platform.PlatformClient;
import vn.mahjonggame.platform.PlatformConfig;
import vn.mahjonggame.platform.ServerMode;
import vn.mahjonggame.util.Common;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.entities.variables.SFSUserVariable;
import com.smartfoxserver.v2.entities.variables.UserVariable;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;

public class OnUserJoinZoneHandler extends BaseServerEventHandler {

	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException {
		// TODO Auto-generated method stub
		User user = (User) event.getParameter(SFSEventParam.USER);
		
		String displayname = (String) user.getSession().getProperty(UserVarName.displayname.toString());
		String avatar = (String) user.getSession().getProperty(UserVarName.avatar.toString());
		String gender = (String) user.getSession().getProperty(UserVarName.gender.toString());		
		String access_token = (String) user.getSession().getProperty(UserVarName.access_token.toString());
		String user_id = (String) user.getSession().getProperty(UserVarName.user_id.toString());
				
		double balance = Double.valueOf(PlatformConfig.configMap.get(PlatformConfig.TUTORIAL_BALANCE));
				
		trace(ExtensionLogLevel.INFO, GameActionType.join_zone.toString() + ": start user_get_balance");
		
		SFSObject sfsRes = PlatformClient.user_get_balance(access_token);
		
		if (Common.isApiSuccess(sfsRes))
		{
			String balanceText = sfsRes.getSFSObject("response").get("balance").getObject().toString();					
			balance = Double.parseDouble(balanceText);
		}
		else {				
			trace(ExtensionLogLevel.ERROR, GameActionType.join_zone.toString() + ": Failed in user_get_balance API.");
		}
		
		trace(ExtensionLogLevel.INFO, GameActionType.join_zone.toString() + ": end user_get_balance");
		
		
		List<UserVariable> vars = new  ArrayList<UserVariable>();
		vars.add( new SFSUserVariable(UserVarName.displayname.toString(), displayname));
		vars.add( new SFSUserVariable(UserVarName.avatar.toString(), avatar));
		vars.add( new SFSUserVariable(UserVarName.gender.toString(), gender));
		/*
		 * Server-side variable
		 */
		SFSUserVariable uvAccessToken =  new SFSUserVariable(UserVarName.access_token.toString(), access_token);
		uvAccessToken.setHidden(true);
		vars.add(uvAccessToken);
		
		SFSUserVariable uvBalance =  new SFSUserVariable(UserVarName.balance.toString(), balance);
		vars.add(uvBalance);
		
		SFSUserVariable uvUserId = new SFSUserVariable(UserVarName.user_id.toString(), user_id);
		vars.add(uvUserId);

		getApi().setUserVariables(user, vars);		
		getApi().subscribeRoomGroup(user, "games");
		
		trace(ExtensionLogLevel.INFO, GameActionType.join_zone.toString() + ": User " + user.getName() + "("+ user.getId()+")");
	}
}
